<?php
/**
 * 塔罗占卜
 */

namespace app\common\lib\tarot;


use app\common\lib\exception\ApiException;

class Tarot
{
    private $tarotType = [ // 牌阵
        1 => [ // 牌阵编号
            'num' => 3, // 牌数量
            'is_dis_position' => 0, // 是否区分牌位
        ],
        2 => [ // 牌阵编号
            'num' => 3, // 牌数量
            'is_dis_position' => 1, // 是否区分牌位
        ],
        3 => [ // 牌阵编号
            'num' => 4, // 牌数量
            'is_dis_position' => 1, // 是否区分牌位
        ],
        4 => [ // 牌阵编号
            'num' => 4, // 牌数量
            'is_dis_position' => 1, // 是否区分牌位
        ],
    ];

    /**
     * 获取牌阵解锁次数
     * @return int
     */
    public function getGetTarotTypeUnlockDecNum(int $tarotType)
    {
        // 提问消耗次数
        $decNumArr = [
            1001 => 2,  // '恋爱圣三角',
            1002 => 2,  // '吉普赛十字牌阵',
            2001 => 1,  // '时间之剑',
            2002 => 1,  // '万能牌阵',
            3001 => 3,  // '每周运势牌阵',
            4001 => 2,  // '身心灵牌阵',
            4002 => 2,  // '四元素牌阵',
            4003 => 3,  // '六芒星牌阵',
            4004 => 1,  // '圣三角',
            5001 => 3, // '灵感对应牌阵',
            6001 => 2, // '抉择牌阵（2选1）',
            6002 => 3, // '抉择牌阵（3选1）',
        ];

        return $decNumArr[$tarotType] ?? 1;
    }

    /**
     * 塔罗占卜
     * @param int $tarotType
     * @return array
     * @throws ApiException
     * @throws \Exception
     */
    public function getTarot(int $tarotType = 1): array
    {
        if (!isset($this->tarotType[$tarotType]))
        {
            throw new ApiException('牌阵缺失；占卜失败', 200, config('code.error'));
        }

        $tarotResult = [];
        $cardList = [];
        // 开始占卜
        for ($i = 1; $i <= $this->tarotType[$tarotType]['num']; $i++)
        {
            do
            {
                $cardId = random_int(1, 78); // 随机抽取牌号
            } while (in_array($cardId, $cardList, true));
            $cardList[] = $cardId;
            $location = $i; // 当前牌位
            $direction = random_int(1, 2); // 牌的正逆  1=>正 2=>逆
            $textId = $this->tarotType[$tarotType]['is_dis_position'] === 1 ? "{$cardId}_{$tarotType}_{$location}_$direction" : "{$cardId}_{$tarotType}_0_$direction";

            $tarotResult[] = [
                'card_id' => $cardId,
                'type' => $tarotType,

                'location' => $location,
                'direction' => $direction,
                'text_id' => $textId,
            ];
        }

        return $tarotResult;
    }

    /**
     * @param array $typeInfo 牌阵信息
     * @param int $isSelectGuide 是否抽 指引牌 1=>抽指引牌 0=>不抽指引牌
     * @param array $cardDataList 已抽的牌信息
     * @return array
     * @throws \Exception
     */
    public function getTarotCard(array $typeInfo, int $isSelectGuide, array $cardDataList = [])
    {
        $cardIds = [];
        $locationData = [];
        foreach ($cardDataList as $item)
        {
            $cardIds[] = $item['card_id'];
            $locationData[] = $item['location'];
        }

        $resCardData = [];
        foreach ($typeInfo as $key => $item)
        {
            if (in_array($key, $locationData, true))
            {   // 该牌位 已经 抽过牌了
                continue;
            }
            if ($isSelectGuide === 0 && $item['tip'] === '指引牌')
            {
                continue;
            }
            do
            {
                $cardId = random_int(1, 78); // 随机抽取牌号
            } while (in_array($cardId, $cardIds, true));
            $cardIds[] = $cardId;
            $location = $key; // 当前牌位
            $direction = random_int(1, 2); // 牌的正逆  1=>正 2=>逆
            $resCardData[] = [
                'card_id' => $cardId,
                'location' => $location,
                'direction' => $direction,
                'status' => 0, // 解锁状态 1=>已解锁 0=>未解锁
            ];
        }

        return $resCardData;
    }

    /**
     * 获取 塔罗详情
     * @param array $tarotConfig
     * @param array $cardDataList
     * @param int $num AI解问题  次数
     * @return array
     */
    public function getTarotInfo(array $tarotConfig, array $cardDataList, int $tarotType, int $num = 0)
    {
        $resData = [];
        foreach ($cardDataList as $item)
        {
            if ($item['status'] === 1)
            {   // 已解锁
                $btnName = '查看AI解读';
            } else
            {   // 未解锁
                // $btnName = $tarotType === 2001 ? 'AI准准解读全部3张牌' : 'AI准准解读';
                $decNum = $this->getGetTarotTypeUnlockDecNum($tarotType);
                $btnName = "消耗{$decNum}次解读牌阵";
                if ($num)
                {
                    $btnName .= "({$num}次)";
                }
            }
            $resData[] = [
                'card_id' => $item['card_id'],
                'direction' => $item['direction'],
                'location' => $item['location'],
                'keyword' => $tarotConfig['card_list'][$item['card_id']]['keyword1'] ?? '',  // 主要代表意向content
                'desc' => $tarotConfig['card_list'][$item['card_id']]['desc'] ?? '',    // 卡牌描述
                'name' => $tarotConfig['card_list'][$item['card_id']]['name'] . '·' . ((int)$item['direction'] === 1 ? '正位' : '逆位'),   // 圣杯国王.正位
                'tip' => $tarotConfig['tarot_type'][$tarotType][$item['location']]['tip'],    // 过去 ｜ 现在 ｜ 未来 ｜星期一
                'status' => $item['status'], // 是否已解锁 1=>已解锁  0=>未解锁
                'btn_name' => $btnName,   // AI准准解读| 查看AI解读
                'img' => "/test/tarot/cards/{$item['card_id']}-{$item['direction']}.png",    // 卡牌 图片
            ];
        }

        return $resData;
    }
}
